
package fy.mdm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.config.FyConfig;
import fy.mdm.bean.MaterialCodeChangeBean;
import fy.msg.MessageSender;
import fy.util.DBUtil;

public class MaterialCodeChangeDao {
	private MaterialCodeChangeBean materialCodeChangeBean;
	private static Log log = LogFactory.getLog(MaterialCodeChangeDao.class);
	// 查下拉值 注意REF_ENUMID正式和测试不同
	private final static String LOOKUP_XIALA_XINGJI = "SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.REF_ENUMID=?";
	public boolean materialCodeChange() throws Exception {
		boolean success = false;
		String msg=null;
		try {
			String ChangeType = null;
			String temp = null;
			String measurementUnitTemp = null;
			String measurementUnit = null;
			
			for (int i = 0; i < materialCodeChangeBean.getOldcodeList().size(); i++) {
				if (materialCodeChangeBean.getChangeType().get(i).equals("0")) {
					ChangeType = "20";
				}
				if (materialCodeChangeBean.getChangeType().get(i).equals("1")) {
					ChangeType = "10";
				}
			if (!materialCodeChangeBean.getNewMeasurement().get(i).equals("")) {
				temp = this.materialCodeChangeBean.getNewMeasurement().get(i);
//				measurementUnitTemp = getUnitCode(temp, "3745152055012904320");//测试
				measurementUnitTemp = getUnitCode(temp, "-8457341316992840107");
				MeasurementUnitMapping mum = new MeasurementUnitMapping();
				measurementUnit=mum.getMeasurementUnit("2", measurementUnitTemp);
				log.info("measurementUnit:" + measurementUnit + "temp" + temp+"measurementUnitTemp"+measurementUnitTemp);
	
			} else {
				log.info("oldMeasurementUnit:" + this.materialCodeChangeBean.getNewMeasurement().get(i));

				measurementUnit =this.materialCodeChangeBean.getNewMeasurement().get(i);
			}		
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
			// 定义请求报文对象
			IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "06001000000012";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
					+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间

			GroupRecord gr1 = new GroupRecord();
			gr1.setName("ROW");
			gr1.setFieldValue("OLD_MATERIEL_CODE", this.materialCodeChangeBean.getOldcodeList().get(i));
			gr1.setFieldValue("NEW_MATERIEL_CODE", this.materialCodeChangeBean.getNewMaterielCode().get(i));
			gr1.setFieldValue("NEW_MATERIEL_DESCRIBE",this.materialCodeChangeBean.getNewMaterielDescribe().get(i));
			gr1.setFieldValue("NEW_MEASUREMENT", measurementUnit);
			gr1.setFieldValue("NEW_MATERIEL_DESCRIBE_EN", this.materialCodeChangeBean.getNewMaterielDescribeEn().get(i));
			gr1.setFieldValue("CHANGE_TYPE", ChangeType);
			gr1.setFieldValue("CHANGE_REASION", this.materialCodeChangeBean.getChangeReasion().get(i));
			gr1.setFieldValue("REMARK", this.materialCodeChangeBean.getRemark().get(i));
			gr1.setFieldValue("EXTEND_C", this.materialCodeChangeBean.getNewModel().get(i));
			gr1.setFieldValue("FONE_DISCRIBE_W", this.materialCodeChangeBean.getNewBrand().get(i));
			gr1.setFieldValue("MAIN_ATTR_DESC_OLD", this.materialCodeChangeBean.getOldmain().get(i));
			gr1.setFieldValue("MAIN_ATTR_DESC_NEW", this.materialCodeChangeBean.getNewmain().get(i));
			

			grs1.add(gr1);
			reqMo.setReqGroupRecord(grs1);
			
			log.info("jo：" +reqMo);
			IMsgObject respMo = null;
			respMo = Requester.execute(reqMo);

			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return success;
	}

	// 获取下拉值
	public String getUnitCode(String code, String temp) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement(LOOKUP_XIALA_XINGJI);
			pst.setString(1, code);
			pst.setString(2, temp);
			rs = pst.executeQuery();
			if (rs.next()) {
				code = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return code;
	}
    private void sendMessage(String user, String msg) {

            MessageSender.sendMsg(user, msg);
        
    }
	

	public MaterialCodeChangeBean getMaterialCodeChangeBean() {
		return materialCodeChangeBean;
	}

	public void setMaterialCodeChangeBean(MaterialCodeChangeBean materialCodeChangeBean) {
		this.materialCodeChangeBean = materialCodeChangeBean;
	}

}
